cc.Class({
    extends: cc.Component,

    properties: {
        timeUp:cc.Label,
        timeAll:60,
        //顾客
        guest:cc.Prefab,
        guestMax:2,
        road:cc.Node,
        car:cc.Prefab,
        bar:cc.Node,

        hamburger:[cc.Prefab], //汉堡包模板 随着升级变化类型
        hamburgerClothes:[cc.SpriteFrame], //烤箱展示 随着升级变化类型
        hamburgerbakeNode:cc.Node, //烤箱节点

        vegetabelShow:[cc.Node], //蔬菜展示 随着升级变化类型
        vegetabelShowSprite1:[cc.SpriteFrame], //蔬菜展示图片 随着升级变化类型
        vegetabelShowSprite2:[cc.SpriteFrame], //蔬菜展示图片 随着升级变化类型
        vegetabelShowSprite3:[cc.SpriteFrame], //蔬菜展示图片 随着升级变化类型
        vegetabelShowSprite4:[cc.SpriteFrame], //蔬菜展示图片 随着升级变化类型

        vegetabelPlace1:[cc.Prefab], //蔬菜拿出 脚本绑定 随着升级变化类型
        vegetabelPlace2:[cc.Prefab], //蔬菜拿出 脚本绑定 随着升级变化类型
        vegetabelPlace3:[cc.Prefab], //蔬菜拿出 脚本绑定 随着升级变化类型
        vegetabelPlace4:[cc.Prefab], //蔬菜拿出 脚本绑定 随着升级变化类型

        drinkNode1:[cc.Prefab], //饮料展示 随着升级变化类型
        drinkNode2:[cc.Prefab], //饮料展示 随着升级变化类型
        drinkNode3:[cc.Prefab], //饮料展示 随着升级变化类型
        drinkNodePlace:cc.Node, //饮料展示 随着升级变化类型

        meat:[cc.Prefab],

        bakeBar:[cc.Prefab],
        bakeBarLock:[cc.Prefab],
        bakeBarPlace:cc.Node,

        plate:[cc.Node],
        plateLock:cc.Node,

        deadLine:cc.Label
    },
    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        //开启物理碰撞
        var manager = cc.director.getCollisionManager();
        manager.enabled = true;
    },
    start () {
        this.init();
        this.timeUp.string = this.timeAll;
        this.startTime = true;
        this.guestPosition=[cc.p(-330,-20),cc.p(72,-20)];
        this.guestComing();
        var time = Math.round(18 + Math.random()*3);
        this.scheduleOnce(function(){
            this.guestComing();
        },time);
        this.schedule(function(){
            this.road.addChild(cc.instantiate(this.car));
        },8);
    },
    init(){
        this.singleDrag = true;
        cc.coinWin = 0;
        cc.diamondWin = 0;
        cc.speedMeat = 1; //加速道具
        cc.speedDrink = 1;
        cc.doubleDiamond = 1;

        this.hamburgerLevel = cc.configData.material.hamburger.level;
        this.meatLevel = cc.configData.material.meat.level;
        this.cheeseLevel = cc.configData.material.cheese.level;
        this.lettuceLevel = cc.configData.material.lettuce.level;
        this.onionLevel = cc.configData.material.onion.level;
        this.tomatoLevel = cc.configData.material.tomato.level;
        this.coffeeLevel = cc.configData.material.coffee.level;
        this.chengzhiLevel = cc.configData.material.chengzhi.level;
        this.keleLevel = cc.configData.material.kele.level;
        this.bakeUnLock = cc.configData.lock.bake.isUpdate;
        this.plateLevel = cc.configData.lock.plate.isUpdate;

        this.hamburgerbakeNode.getComponent(cc.Sprite).spriteFrame = this.hamburgerClothes[this.hamburgerLevel];

        this.vegetabelShow[0].getComponent(cc.Sprite).spriteFrame = this.vegetabelShowSprite1[this.tomatoLevel];
        this.vegetabelShow[0].addChild(cc.instantiate(this.vegetabelPlace1[this.tomatoLevel]));
        this.vegetabelShow[1].getComponent(cc.Sprite).spriteFrame = this.vegetabelShowSprite2[this.onionLevel];
        this.vegetabelShow[1].addChild(cc.instantiate(this.vegetabelPlace2[this.onionLevel]));
        this.vegetabelShow[2].getComponent(cc.Sprite).spriteFrame = this.vegetabelShowSprite3[this.lettuceLevel];
        this.vegetabelShow[2].addChild(cc.instantiate(this.vegetabelPlace3[this.lettuceLevel]));
        this.vegetabelShow[3].getComponent(cc.Sprite).spriteFrame = this.vegetabelShowSprite4[this.cheeseLevel];
        this.vegetabelShow[3].addChild(cc.instantiate(this.vegetabelPlace4[this.cheeseLevel]));

        this.drinkNodePlace.addChild(cc.instantiate(this.drinkNode1[this.coffeeLevel]));
        this.drinkNodePlace.addChild(cc.instantiate(this.drinkNode2[this.chengzhiLevel]));
        this.drinkNodePlace.addChild(cc.instantiate(this.drinkNode3[this.keleLevel]));

        if(!this.bakeUnLock){
            this.bakeBarPlace.addChild(cc.instantiate(this.bakeBarLock[this.meatLevel]));
        }else{
            var bakebakeBar1 = cc.instantiate(this.bakeBar[this.meatLevel]);
            this.bakeBarPlace.addChild(bakebakeBar1);
            bakebakeBar1.place = true;
            bakebakeBar1.on('click',function(){
                if(!bakebakeBar1.place) return;
                if(bakebakeBar1.childrenCount >0) return;
                cc.voicePlay.playEffect('click_game');
                var meat = cc.instantiate(this.meat[this.meatLevel]);
                bakebakeBar1.addChild(meat);
                bakebakeBar1.place = false
                if(this.meatLevel === 0){
                    meat.setPosition(cc.p(-10,10));
                }
            },this);
        }
        var bakebakeBar2 = cc.instantiate(this.bakeBar[this.meatLevel]);
        this.bakeBarPlace.addChild(bakebakeBar2);
        bakebakeBar2.place = true;
        bakebakeBar2.on('click',function(){
            if(!bakebakeBar2.place) return;
            if(bakebakeBar2.childrenCount >0) return;
            cc.voicePlay.playEffect('click_game');
            var meat = cc.instantiate(this.meat[this.meatLevel]);
            bakebakeBar2.addChild(meat);
            bakebakeBar2.place = false
            if(this.meatLevel === 0){
                meat.setPosition(cc.p(-10,10));
            }
        },this);
        var bakebakeBar3 = cc.instantiate(this.bakeBar[this.meatLevel]);
        this.bakeBarPlace.addChild(bakebakeBar3);
        bakebakeBar3.place = true;
        bakebakeBar3.on('click',function(){
            if(!bakebakeBar3.place) return;
            if(bakebakeBar3.childrenCount >0) return;
            cc.voicePlay.playEffect('click_game');
            var meat = cc.instantiate(this.meat[this.meatLevel]);
            bakebakeBar3.addChild(meat);
            bakebakeBar3.place = false
            if(this.meatLevel === 0){
                meat.setPosition(cc.p(-10,10));
            }
        },this);
        this.plate[0].place = true
        this.plate[1].place = true
        if(this.plateLevel){
            this.plateLock.destroy();
        }

     this.jishi = 1;
    },
    coinShow(){

    },
    //出来顾客 20s之内不能获得食品就走了 12s之后开始态度转变（显得不耐烦）  一个顾客走了之后 2-5s只间来另一个顾客  同时最多两个顾客
    guestComing(){
        if(this.guestPosition.length <= 0) return;
        var guest = cc.instantiate(this.guest);
        this.bar.addChild(guest);
        guest.setPosition(-630,-20);
        var ran = Math.floor(Math.random()*this.guestPosition.length);
        var po = this.guestPosition.splice(ran,1)[0];
        if(po.x > 0){
          var time = 2;
        }else{
            var time = 1;
        }
        guest.runAction(cc.repeatForever(cc.sequence(cc.moveBy(0.15,0,10),cc.moveBy(0.15,0,-10))));
        guest.runAction(cc.sequence(cc.moveTo(time,po),cc.callFunc(function(){
            guest.stopAllActions();
            guest.setPosition(po);
            guest.children[0].active=true;
            guest.group = 'person';
            guest.getComponent('guest').startTime = true;
        },this)));
    },
    addHamburger(){
        cc.voicePlay.playEffect('click_game');
        console.log(this.plate[0].place);
        if(this.plate[0].childrenCount == 0){
            if(!this.plate[0].place) return;
            this.plate[0].addChild(cc.instantiate(this.hamburger[this.hamburgerLevel]));
            this.plate[0].place =false;
            return;
        }
        if(this.plate[1].childrenCount == 0){
            if(!this.plate[1].place) return;
            this.plate[1].addChild(cc.instantiate(this.hamburger[this.hamburgerLevel]));
            this.plate[1].place =false;
        }
    },
    addMeat(event,Index){
        // console.log(Index);//坐标用的
        if(event.target.childrenCount == 0){
            event.target.addChild(cc.instantiate(this.meat[this.meatLevel]));
        }
    },
    playvoice(){
        cc.voicePlay.playEffect('time321');
        this.deadLine.string = Math.floor(this.timeAll)
        this.jishi = 0;
    },
    //总的时间1分钟
    updateTime(dt){

        if(this.timeAll <= 4 && this.timeAll >= 1){
         this.jishi += dt;
         if(this.jishi >= 1){
             this.playvoice();
         }
        }
        if(this.timeAll <= 0){
            this.startTime = false;
            this.deadLine.string = '';
            console.log('时间到了',cc.coinWin);
            console.log('时间到了',cc.diamondWin);
            cc.find("Canvas/collectDiamond").active= true;
            return;
        }
        this.timeUp.string = Math.floor(this.timeAll);
    },
    update (dt) {
        if(!this.startTime) return;
        this.timeAll -= dt;
        this.updateTime(dt)
    },
});
